package hn.cch.spring.core;

import hn.cch.spring.core.annotation.component.BeanImplement0;
import hn.cch.spring.core.annotation.component.BeanImplement1;
import hn.cch.spring.core.bean.BeanComponent;
import hn.cch.spring.core.bean.BeanDependencies;
import hn.cch.spring.core.bean.JavaProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.context.support.GenericApplicationContext;

public class BeanFactoryPostProcessorDemo {

    private static final Logger logger = LoggerFactory.getLogger(BeanFactoryPostProcessorDemo.class);


    public static void main(String[] args) {
        // BeanFactoryPostProcessor解析对应注解
        GenericApplicationContext applicationContext = new GenericApplicationContext();

        applicationContext.registerBean("beanDependency", BeanDependencies.class);
        applicationContext.registerBean("beanImplement0", BeanImplement0.class);
        applicationContext.registerBean("beanImplement1", BeanImplement1.class);
        applicationContext.registerBean("beanComponent", BeanComponent.class);
        applicationContext.registerBean("javaProperties", JavaProperties.class);


        applicationContext.getDefaultListableBeanFactory()
            .setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());

        applicationContext.registerBean(AutowiredAnnotationBeanPostProcessor.class); // @Autowired @Value
        applicationContext.registerBean(CommonAnnotationBeanPostProcessor.class); // @Resource @PostConstruct @PreDestroy
        applicationContext.registerBean(ConfigurationClassPostProcessor.class);// @ComponentScan @Bean @Import @ImportResource

        // @ConfigurationProperties
        ConfigurationPropertiesBindingPostProcessor.register(applicationContext.getDefaultListableBeanFactory());

        applicationContext.refresh();

        BeanComponent beanComponent = applicationContext.getBean(BeanComponent.class);
        logger.info(beanComponent.toString());

        JavaProperties javaProperties = applicationContext.getBean(JavaProperties.class);
        logger.info("JavaProperties:home={},version={}.", javaProperties.getHome(), javaProperties.getVersion());

        applicationContext.close();
    }

}
